﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web.Routing;

using ProjectBase.Core;
using ProjectBase.Core.Web;

namespace ProjectBase.Mvc
{
    public abstract class DetailLinkControl<TSourceProperty, TPropertyControl> : TextPropertyControl<TSourceProperty, TPropertyControl>
        where TPropertyControl : DetailLinkControl<TSourceProperty, TPropertyControl>
    {
        #region members
        readonly IDictionary<string, object> _routeValues = new Dictionary<string, object>();
        string _detailUrl;
        bool _hasDetailLink = true;
        int _maxLength = 40;
        string _notFoundOrNullTemplate = Constants.WebControls.Common.NotFound;
        #endregion members

        #region constructor
        protected DetailLinkControl(bool? asReadonly = null) : base(asReadonly) { }
        protected DetailLinkControl(string prefix) : base(prefix) { }
        #endregion constructor

        #region OnSourceProperty
        protected override void OnSourcePropertyChanged(string propertyName, TSourceProperty value)
        {
            base.OnSourcePropertyChanged(propertyName, value);

            SetDetailController(Type.Name);
        }
        #endregion OnSourceProperty

        #region public Set()
        public virtual TPropertyControl SetNotFoundOrNullTemplate(string notFoundOrNullTemplate)
        {
            NotFoundOrNullTemplate = notFoundOrNullTemplate;
            return this as TPropertyControl;
        }

        public virtual TPropertyControl SetDetailUrl(string detailUrl)
        {
            DetailUrl = detailUrl;
            return this as TPropertyControl;
        }

        public virtual TPropertyControl SetDetailController(string detailController)
        {
            DetailController = detailController;
            return this as TPropertyControl;
        }

        public virtual TPropertyControl SetDetailAction(string detailAction)
        {
            DetailAction = detailAction;
            return this as TPropertyControl;
        }

        public virtual TPropertyControl SetDetailEntityID(string detailEntityID)
        {
            DetailEntityID = detailEntityID;
            return this as TPropertyControl;
        }

        public virtual TPropertyControl HideLinkToDetail(bool hide)
        {
            HasDetailLink = !hide;
            return this as TPropertyControl;
        }

        public virtual TPropertyControl SetTitle(string title)
        {
            Title = title;
            return this as TPropertyControl;
        }

        public virtual TPropertyControl SetMaxLength(short maxLength)
        {
            MaxLength = maxLength;
            return this as TPropertyControl;
        }

        public virtual TPropertyControl AddRouteValue(string key, object value)
        {
            if (key.IsNotEmpty()
                && value.Is())
            {
                RouteValues[key] = value;
            }
            return this as TPropertyControl;
        }
        #endregion public Set()

        #region protected Create RouteValues
        protected virtual RouteValueDictionary CreateRouteValues()
        {
            var rvd = new RouteValueDictionary { { "id", DetailEntityID } };
            foreach (var pair in RouteValues)
            {
                rvd[pair.Key] = pair.Value;
            }
            return rvd;
        }
        #endregion protected Create RouteValues

        #region properties
        /// <summary>
        ///     If filter action requires other parameters, append them here
        /// </summary>
        public virtual IDictionary<string, object> RouteValues
        {
            get { return _routeValues; }
        }

        public virtual bool HasDetailLink
        {
            get { return _hasDetailLink; }
            set { _hasDetailLink = value; }
        }

        public virtual string NotFoundOrNullTemplate
        {
            get { return _notFoundOrNullTemplate; }
            set { _notFoundOrNullTemplate = value; }
        }

        public virtual string DetailUrl
        {
            get
            {
                if (_detailUrl.IsNotEmpty())
                {
                    return _detailUrl;
                }
                if (DetailController.IsEmpty()
                    || DetailEntityID.IsEmpty())
                {
                    return null;
                }

                return Url.Action(
                    DetailAction ?? Constants.Actions.Detail,
                    DetailController,
                    CreateRouteValues());
            }
            set { _detailUrl = value; }
        }

        public virtual string DetailController { get; set; }
        public virtual string DetailAction { get; set; }
        public virtual string DetailEntityID { get; set; }

        public override string Text
        {
            get { return base.Text.IsEmpty() ? NotFoundOrNullTemplate : base.Text; }
            set { base.Text = value; }
        }

        /// <summary>
        ///     If not set, title="Text" attribute is rendered, 
        ///     when Text size is larger then MaxLength
        /// </summary>
        public virtual string Title { get; set; }

        /// <summary>
        ///     Allows to restrict the MaxLength of the Text value.
        ///     Default value is 20.
        /// </summary>
        public virtual int MaxLength
        {
            get { return _maxLength; }
            set { _maxLength = value; }
        }
        #endregion properties
    }
}